package modele;


import java.util.HashMap;
import java.util.List;

import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;


@Stateless
@TransactionManagement(TransactionManagementType.BEAN)
public class FacadeMenu {

	@PersistenceContext
	private EntityManager em;
		
	public HashMap<String,List<Competition>> getMenu() {
		
		HashMap<String,List<Competition>> hash = new HashMap<String,List<Competition>>();
		
		CriteriaBuilder builder = em.getCriteriaBuilder();
		CriteriaQuery<Sport> criteria = builder.createQuery( Sport.class );
		Root<Sport> menuRoot = criteria.from( Sport.class );
		criteria.select(menuRoot);
		List<Sport> resultat = em.createQuery(criteria).getResultList();
		
		for (Sport sport : resultat){
			hash.put(sport.getName(), (List<Competition>) sport.getCompetitions());
		}
		return hash;
	}
	
	
	public List<Sport> getListSport() {
		
		CriteriaBuilder builder = em.getCriteriaBuilder();
		CriteriaQuery<Sport> criteria = builder.createQuery( Sport.class );
		Root<Sport> menuRoot = criteria.from( Sport.class );
		criteria.select(menuRoot);
		return em.createQuery(criteria).getResultList();
		
	}
	
	public List<Competition> getListCompetition() {
		
		CriteriaBuilder builder = em.getCriteriaBuilder();
		CriteriaQuery<Competition> criteria = builder.createQuery( Competition.class );
		Root<Competition> menuRoot = criteria.from( Competition.class );
		criteria.select(menuRoot);
		return em.createQuery(criteria).getResultList();
		
	}
	
	public String getNameSport(Long idSport) {
		Sport s = em.find(Sport.class, idSport);
		if (s==null){
			return null;
		}
		else{
			return s.getName();
		}
		
	}
	
}
